import React, {useState} from "react";

export default function Count() {
  const [count, setCount] = useState(0)
  const selectedRef = React.createRef()

  const plus=()=>{
    const {value} = selectedRef.current
    setCount(count+parseInt(value))
  }
  const minus=()=>{
    const {value} = selectedRef.current
    setCount(count-parseInt(value))
  }
  const plusIfOdd=()=>{
    const {value} = selectedRef.current
    if(count%2===1)
      setCount(count+parseInt(value))
  }
  const plusAsync=()=>{
    setTimeout(()=>{
      const {value} = selectedRef.current
      setCount(count+parseInt(value))
    }, 2000)
  }

  return (
    <div>
      <p>结果: {count}</p>
      <select ref={selectedRef}>
        <option value="1">1</option>
        <option value="2">2</option>
        <option value="3">3</option>
      </select>&nbsp;
      <button onClick={plus}>+</button>&nbsp;
      <button onClick={minus}>-</button>&nbsp;
      <button onClick={plusIfOdd}>是奇数才加</button>&nbsp;
      <button onClick={plusAsync}>异步加</button>&nbsp;
    </div>
  );
}
